package com.example.utils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

public class TakePhoto
{
	public static String typeStr;
	/* 用来标识请求照相功能的activity */
	public static final int CAMERA_WITH_DATA = 3023;
	/* 用来标识请求gallery的activity */
	public static final int PHOTO_PICKED_WITH_DATA = 3021;
	/* 表示裁剪图片的activity */
	public static final int CROP_PHOTO = 3022;
	/* 表示从相册裁剪图片的activity */
	public static final int PHOTO_CROP_PHOTO = 3020;
	/* 拍照的照片存储位置 */
	// private static final File PHOTO_DIR = new
	// File(Environment.getExternalStorageDirectory() +
	// "/DCIM/Camera");
	public static File mCurrentPhotoFile;// 照相机拍照得到的图片
	private static File restFile; // 餐厅图片文件
	
	public static void setRestFile()
	{
		File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");
		long time = System.currentTimeMillis();
		SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG_'yyyyMMdd");
		String name = String.valueOf(time);
		name = dateFormat.format(new Date(time)) + "_" + name.substring(name.length() - 6, name.length()) + ".jpg";
		try
		{
			restFile = new File(file, name);
			if (!restFile.exists())
				restFile.createNewFile();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public static File getRestFile()
	{
		return restFile;
	}
	public static void TakeRestPhoto(Activity activity, int type)
	{
		setRestFile();
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getRestFile()));
		if (type == 0)
			typeStr = "front";
		else if (type == 1)
			typeStr = "back";
		activity.startActivityForResult(intent, TakePhoto.CAMERA_WITH_DATA);
	}
	public static void cropRestPhoto(Activity activity)
	{
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(Uri.fromFile(getRestFile()), "image/*");
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getRestFile()));
		activity.startActivityForResult(intent, CROP_PHOTO);
	}
	public static void cropRestPhotoByUri(Activity activity, String filePath)
	{
		restFile = new File(filePath);
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(Uri.fromFile(getRestFile()), "image/*");
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getRestFile()));
		activity.startActivityForResult(intent, CROP_PHOTO);
	}
	public static void getRestPhoto(Activity activity, int type)
	{
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		intent.setType("image/*");
		if (type == 0)
			typeStr = "front";
		else if (type == 1)
			typeStr = "back";
		activity.startActivityForResult(intent, TakePhoto.PHOTO_PICKED_WITH_DATA);
	}
	/**
	 * 用当前时间给取得的图片命名
	 */
	// public static String getPhotoFileName() {
	// Date date = new Date(System.currentTimeMillis());
	// return ZDStringUtil.SD_YMDHMS.format(date) + ".jpg";
	// }
	// 封装请求Gallery的intent
	public static Intent getPhotoPickIntent()
	{
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		intent.setType("image/*");
		setIntentExtra(intent);
		return intent;
	}
	/**
	 * Constructs an intent for image cropping. 调用图片剪辑程序
	 */
	public static Intent getCropImageIntent(Uri photoUri)
	{
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(photoUri, "image/*");
		setIntentExtra(intent);
		return intent;
	}
	public static void doTakePhoto(Activity activity)
	{
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		activity.startActivityForResult(intent, TakePhoto.CAMERA_WITH_DATA);
	}
	public static Intent getCropImageIntent(Bitmap data)
	{
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setType("image/*");
		intent.putExtra("data", data);
		setIntentExtra(intent);
		return intent;
	}
	public static void cropRestPhotos(Activity activity, File photoUri)
	{
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(Uri.fromFile(photoUri), "image/*");
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoUri));
		// setIntentExtra(intent);
		activity.startActivityForResult(intent, PHOTO_CROP_PHOTO);
	}
	private static void setIntentExtra(Intent intent)
	{
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 80);
		intent.putExtra("outputY", 80);
		intent.putExtra("return-data", true);
	}
	public static Bitmap buildBitmapByFilePath(Activity c, String filePath)
	{
		int dw = c.getWindowManager().getDefaultDisplay().getWidth();
		int dh = c.getWindowManager().getDefaultDisplay().getHeight() / 2;
		// 已屏幕宽 和一般的高作为图片显示的最大尺寸
		try
		{
			BitmapFactory.Options factory = new BitmapFactory.Options();
			factory.inJustDecodeBounds = true; // 当为true时 允许查询图片不为 图片像素分配内存
			Bitmap bmp = BitmapFactory.decodeFile(filePath, factory);
			int hRatio = (int) Math.ceil(factory.outHeight / (float) dh); // 图片是高度的几倍
			int wRatio = (int) Math.ceil(factory.outWidth / (float) dw); // 图片是宽度的几倍
			// System.out.println("hRatio:" + hRatio + "  wRatio:" + wRatio);
			// 缩小到 1/ratio的尺寸和 1/ratio^2的像素
			if (hRatio > 1 || wRatio > 1)
			{
				if (hRatio > wRatio)
				{
					factory.inSampleSize = hRatio;
				}
				else
					factory.inSampleSize = wRatio;
			}
			factory.inPurgeable = true;
			factory.inInputShareable = true;
			factory.inJustDecodeBounds = false;
			if (filePath.startsWith("http"))
			{
				URL url = new URL(filePath);
				URLConnection conn = url.openConnection();
				conn.connect();
				bmp = BitmapFactory.decodeStream(conn.getInputStream(), null, factory);
			}
			else
			{
				bmp = BitmapFactory.decodeFile(filePath, factory);
			}
			return bmp;
		}
		catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
}
